import { resolve } from 'path'
import fs from 'fs'
import compressing from 'compressing'

const zipPath = resolve('zip')
// 压缩包名，可以根据实际项目命名
const zipName = (() => 'zip/vue3-h5-template.zip')()

// 判断是否存在当前zip路径，没有就新增
if (!fs.existsSync(zipPath)) {
  fs.mkdirSync(zipPath)
}

// 清空 zip 目录
const zipDirs = fs.readdirSync('./zip')
if (zipDirs && zipDirs.length > 0) {
  for (let index = 0; index < zipDirs.length; index++) {
    const dir = zipDirs[index]
    const dirPath = resolve('zip/' + dir)
    console.log('del ===', dirPath)
    fs.unlinkSync(dirPath)
  }
}

compressing.zip
  .compressDir(resolve('./dist'), resolve(zipName), {
    // ignoreBase为false的话，压缩包里包含dist文件夹目录，为true的话压缩包里不包含dist文件夹目录
    ignoreBase: false,
  })
  .then(() => {
    console.log(`Tip: 文件压缩成功，已压缩至【${resolve(zipName)}】`)
  })
  .catch((err) => {
    console.log('Tip: 压缩报错')
    console.error(err)
  })
